import { UserController, TeacherController } from './controller'

const fnDebugLog = require('debug')('main')

class Module {
  public controllersList: any[] = [ ]
  constructor (ctrlClassList: any[]) {
    ctrlClassList.forEach((ctrlClass)=> {
      this.controllersList.push(new ctrlClass())
    })
  }
}

const requestsList = [
  { method: 'get', route: 'users',      params: 'age>10'},
  { method: 'get', route: 'users/vip',  params: 'name=mike'},
  { method: 'get', route: 'teachers',   params: 'age<40'},
]

function main () {
  const m = new Module([UserController, TeacherController])

  const list = m.controllersList;

  requestsList.forEach((req) => {
    fnDebugLog('+++++++', req);
    for (let controller of list) {
      if (req.route.indexOf(controller.root) !== 0) continue;
      fnDebugLog(controller, controller.root, controller.handlersOfGet)
      controller.handlersOfGet?.forEach((handler:any) => {
        
        if (!!handler.route) {
          fnDebugLog(req.route, `${controller.root}/${handler.route}`)
          if (req.route !== `${controller.root}/${handler.route}`) return;
        } else {
          fnDebugLog(req.route, `${controller.root}`)
        }
        const result = controller[handler.method](req.params)
        fnDebugLog('result', result)
      })
    }

  })
}

main()
